/*     */ package net.gxf.nehe.learn;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ import java.nio.ByteOrder;
/*     */ import java.nio.IntBuffer;
/*     */ import java.util.StringTokenizer;
/*     */ import org.lwjgl.input.Keyboard;
/*     */ import org.lwjgl.opengl.Display;
/*     */ import org.lwjgl.opengl.DisplayMode;
/*     */ import org.lwjgl.opengl.GL11;
/*     */ import org.lwjgl.util.glu.GLU;
/*     */ import org.newdawn.slick.opengl.Texture;
/*     */ import org.newdawn.slick.opengl.TextureLoader;
/*     */ import org.newdawn.slick.util.ResourceLoader;
/*     */ 
/*     */ public class Lesson10
/*     */ {
/*  46 */   private boolean done = false;
/*  47 */   private boolean fullscreen = false;
/*  48 */   private final String windowTitle = "NeHe's OpenGL Lesson 10 for LWJGL (Loading And Moving Through A 3D World)";
/*  49 */   private boolean f1 = false;
/*     */   private DisplayMode displayMode;
/*     */   private boolean blend;
/*     */   private boolean bp;
/*     */   private boolean fp;
/*  55 */   private final float piover180 = 0.01745329F;
/*     */   private float heading;
/*     */   private float xpos;
/*     */   private float zpos;
/*     */   private float yrot;
/*  60 */   private float walkbias = 0.0F;
/*  61 */   private float walkbiasangle = 0.0F;
/*  62 */   private float lookupdown = 0.0F;
/*  63 */   private float z = 0.0F;
/*     */   private int filter;
/*     */   private Texture texture;
/*     */   private Sector sector1;
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/*  71 */     boolean fullscreen = false;
/*  72 */     if ((args.length > 0) && 
/*  73 */       (args[0].equalsIgnoreCase("fullscreen"))) {
/*  74 */       fullscreen = true;
/*     */     }
/*     */ 
/*  78 */     Lesson10 l10 = new Lesson10();
/*  79 */     l10.run(fullscreen);
/*     */   }
/*     */   public void run(boolean fullscreen) {
/*  82 */     this.fullscreen = fullscreen;
/*     */     try {
/*  84 */       init();
/*  85 */       while (!this.done) {
/*  86 */         mainloop();
/*  87 */         render();
/*  88 */         Display.update();
/*     */       }
/*  90 */       cleanup();
/*     */     }
/*     */     catch (Exception e) {
/*  93 */       e.printStackTrace();
/*  94 */       System.exit(0);
/*     */     }
/*     */   }
/*     */ 
/*  98 */   private void mainloop() { if (Keyboard.isKeyDown(1)) {
/*  99 */       this.done = true;
/*     */     }
/* 101 */     if (Display.isCloseRequested()) {
/* 102 */       this.done = true;
/*     */     }
/* 104 */     if ((Keyboard.isKeyDown(59)) && (!this.f1)) {
/* 105 */       this.f1 = true;
/* 106 */       switchMode();
/*     */     }
/* 108 */     if (!Keyboard.isKeyDown(59)) {
/* 109 */       this.f1 = false;
/*     */     }
/* 111 */     if ((Keyboard.isKeyDown(48)) && (!this.bp)) {
/* 112 */       this.bp = true;
/* 113 */       this.blend = (!this.blend);
/* 114 */       if (!this.blend) {
/* 115 */         GL11.glDisable(3042);
/* 116 */         GL11.glEnable(2929);
/*     */       }
/*     */       else {
/* 119 */         GL11.glEnable(3042);
/* 120 */         GL11.glDisable(2929);
/*     */       }
/*     */     }
/* 123 */     if (!Keyboard.isKeyDown(48)) {
/* 124 */       this.bp = false;
/*     */     }
/*     */ 
/* 127 */     if ((Keyboard.isKeyDown(33)) && (!this.fp)) {
/* 128 */       this.fp = true;
/* 129 */       this.filter += 1;
/* 130 */       if (this.filter > 2) {
/* 131 */         this.filter = 0;
/*     */       }
/*     */     }
/* 134 */     if (!Keyboard.isKeyDown(33)) {
/* 135 */       this.fp = false;
/*     */     }
/*     */ 
/* 138 */     if (Keyboard.isKeyDown(201)) {
/* 139 */       this.z -= 0.02F;
/*     */     }
/*     */ 
/* 142 */     if (Keyboard.isKeyDown(209)) {
/* 143 */       this.z += 0.02F;
/*     */     }
/*     */ 
/* 146 */     if (Keyboard.isKeyDown(200))
/*     */     {
/* 148 */       this.xpos -= (float)Math.sin(this.heading * 0.01745329F) * 0.05F;
/* 149 */       this.zpos -= (float)Math.cos(this.heading * 0.01745329F) * 0.05F;
/* 150 */       if (this.walkbiasangle >= 359.0F) {
/* 151 */         this.walkbiasangle = 0.0F;
/*     */       }
/*     */       else {
/* 154 */         this.walkbiasangle += 10.0F;
/*     */       }
/* 156 */       this.walkbias = ((float)Math.sin(this.walkbiasangle * 0.01745329F) / 20.0F);
/*     */     }
/*     */ 
/* 159 */     if (Keyboard.isKeyDown(208)) {
/* 160 */       this.xpos += (float)Math.sin(this.heading * 0.01745329F) * 0.05F;
/* 161 */       this.zpos += (float)Math.cos(this.heading * 0.01745329F) * 0.05F;
/* 162 */       if (this.walkbiasangle <= 1.0F) {
/* 163 */         this.walkbiasangle = 359.0F;
/*     */       }
/*     */       else {
/* 166 */         this.walkbiasangle -= 10.0F;
/*     */       }
/* 168 */       this.walkbias = ((float)Math.sin(this.walkbiasangle * 0.01745329F) / 20.0F);
/*     */     }
/*     */ 
/* 171 */     if (Keyboard.isKeyDown(205)) {
/* 172 */       this.heading -= 1.0F;
/* 173 */       this.yrot = this.heading;
/*     */     }
/*     */ 
/* 176 */     if (Keyboard.isKeyDown(203)) {
/* 177 */       this.heading += 1.0F;
/* 178 */       this.yrot = this.heading;
/*     */     }
/*     */ 
/* 181 */     if (Keyboard.isKeyDown(201)) {
/* 182 */       this.lookupdown -= 1.0F;
/*     */     }
/*     */ 
/* 185 */     if (Keyboard.isKeyDown(209))
/* 186 */       this.lookupdown += 1.0F; }
/*     */ 
/*     */   private void switchMode()
/*     */   {
/* 190 */     this.fullscreen = (!this.fullscreen);
/*     */     try {
/* 192 */       Display.setFullscreen(this.fullscreen);
/*     */     }
/*     */     catch (Exception e) {
/* 195 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   private boolean render() {
/* 200 */     GL11.glClear(16640);
/* 201 */     GL11.glLoadIdentity();
/*     */ 
/* 204 */     float xtrans = -this.xpos;
/* 205 */     float ztrans = -this.zpos;
/* 206 */     float ytrans = -this.walkbias - 0.25F;
/* 207 */     float sceneroty = 360.0F - this.yrot;
/*     */ 
/* 211 */     GL11.glRotatef(this.lookupdown, 1.0F, 0.0F, 0.0F);
/* 212 */     GL11.glRotatef(sceneroty, 0.0F, 1.0F, 0.0F);
/*     */ 
/* 214 */     GL11.glTranslatef(xtrans, ytrans, ztrans);
/* 215 */     GL11.glBindTexture(3553, this.texture.getTextureID());
/*     */ 
/* 217 */     int numTriangles = this.sector1.numTriangles;
/*     */ 
/* 220 */     for (int loop_m = 0; loop_m < numTriangles; loop_m++)
/*     */     {
/* 222 */       GL11.glBegin(4);
/* 223 */       GL11.glNormal3f(0.0F, 0.0F, 1.0F);
/* 224 */       float x_m = this.sector1.triangle[loop_m].vertex[0].x;
/* 225 */       float y_m = this.sector1.triangle[loop_m].vertex[0].y;
/* 226 */       float z_m = this.sector1.triangle[loop_m].vertex[0].z;
/* 227 */       float u_m = this.sector1.triangle[loop_m].vertex[0].u;
/* 228 */       float v_m = this.sector1.triangle[loop_m].vertex[0].v;
/* 229 */       GL11.glTexCoord2f(u_m, v_m); GL11.glVertex3f(x_m, y_m, z_m);
/*     */ 
/* 231 */       x_m = this.sector1.triangle[loop_m].vertex[1].x;
/* 232 */       y_m = this.sector1.triangle[loop_m].vertex[1].y;
/* 233 */       z_m = this.sector1.triangle[loop_m].vertex[1].z;
/* 234 */       u_m = this.sector1.triangle[loop_m].vertex[1].u;
/* 235 */       v_m = this.sector1.triangle[loop_m].vertex[1].v;
/* 236 */       GL11.glTexCoord2f(u_m, v_m); GL11.glVertex3f(x_m, y_m, z_m);
/*     */ 
/* 238 */       x_m = this.sector1.triangle[loop_m].vertex[2].x;
/* 239 */       y_m = this.sector1.triangle[loop_m].vertex[2].y;
/* 240 */       z_m = this.sector1.triangle[loop_m].vertex[2].z;
/* 241 */       u_m = this.sector1.triangle[loop_m].vertex[2].u;
/* 242 */       v_m = this.sector1.triangle[loop_m].vertex[2].v;
/* 243 */       GL11.glTexCoord2f(u_m, v_m); GL11.glVertex3f(x_m, y_m, z_m);
/* 244 */       GL11.glEnd();
/*     */     }
/* 246 */     return true;
/*     */   }
/*     */   private void createWindow() throws Exception {
/* 249 */     Display.setFullscreen(this.fullscreen);
/* 250 */     DisplayMode[] d = Display.getAvailableDisplayModes();
/* 251 */     for (int i = 0; i < d.length; i++) {
/* 252 */       if ((d[i].getWidth() == 640) && (d[i].getHeight() == 480) && (d[i].getBitsPerPixel() == 32))
/*     */       {
/* 255 */         this.displayMode = d[i];
/* 256 */         break;
/*     */       }
/*     */     }
/* 259 */     Display.setDisplayMode(this.displayMode);
/* 260 */     Display.setTitle("NeHe's OpenGL Lesson 10 for LWJGL (Loading And Moving Through A 3D World)");
/* 261 */     Display.create();
/*     */   }
/*     */   private void init() throws Exception {
/* 264 */     createWindow();
/*     */ 
/* 266 */     setupWorld();
/*     */ 
/* 268 */     loadTextures();
/* 269 */     initGL();
/*     */   }
/*     */ 
/*     */   private void loadTextures() throws IOException
/*     */   {
/* 274 */     this.texture = loadTexture("Data/psu.jpeg");
/*     */   }
/*     */   private void initGL() {
/* 277 */     GL11.glEnable(3553);
/* 278 */     GL11.glShadeModel(7425);
/* 279 */     GL11.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
/* 280 */     GL11.glClearDepth(1.0D);
/*     */ 
/* 282 */     GL11.glHint(3152, 4354);
/*     */ 
/* 284 */     GL11.glMatrixMode(5889);
/* 285 */     GL11.glLoadIdentity();
/*     */ 
/* 288 */     GLU.gluPerspective(45.0F, this.displayMode.getWidth() / this.displayMode.getHeight(), 0.1F, 100.0F);
/*     */ 
/* 291 */     GL11.glMatrixMode(5888);
/*     */   }
/*     */   private static void cleanup() {
/* 294 */     Display.destroy();
/*     */   }
/*     */ 
/*     */   private Texture loadTexture(String path)
/*     */     throws IOException
/*     */   {
/* 300 */     this.texture = TextureLoader.getTexture("BMP", ResourceLoader.getResourceAsStream(path));
/* 301 */     IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
/* 302 */     ByteBuffer scratch = ByteBuffer.allocateDirect(this.texture.getImageHeight() * this.texture.getImageWidth() * 3);
/*     */ 
/* 305 */     IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
/* 306 */     GL11.glGenTextures(buf);
/*     */ 
/* 308 */     GL11.glBindTexture(3553, buf.get(0));
/*     */ 
/* 312 */     GL11.glTexParameteri(3553, 10241, 9729);
/*     */ 
/* 314 */     GL11.glTexParameteri(3553, 10240, 9729);
/*     */ 
/* 316 */     GL11.glTexImage2D(3553, 0, 6407, this.texture.getImageWidth(), this.texture.getImageHeight(), 0, 6407, 5121, scratch);
/*     */ 
/* 319 */     return this.texture;
/*     */   }
/*     */ 
/*     */   private final void setupWorld()
/*     */   {
/*     */     try
/*     */     {
/* 328 */       BufferedReader dis = new BufferedReader(new FileReader("data/world.txt"));
/*     */       String line;
/* 330 */       while ((line = dis.readLine()) != null) {
/* 331 */         if ((line.trim().length() != 0) && (!line.trim().startsWith("//")) && 
/* 334 */           (line.startsWith("NUMPOLLIES")))
/*     */         {
/* 337 */           int numTriangles = Integer.parseInt(line.substring(line.indexOf("NUMPOLLIES") + "NUMPOLLIES".length() + 1));
/* 338 */           this.sector1 = new Sector(numTriangles);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 344 */       for (int i = 0; i < this.sector1.numTriangles; i++) {
/* 345 */         for (int vert = 0; vert < 3; vert++)
/*     */         {
/* 347 */           while (((line = dis.readLine()) != null) && (
/* 348 */             (line.trim().length() == 0) || (line.trim().startsWith("//"))));
/* 354 */           if (line != null) {
/* 355 */             StringTokenizer st = new StringTokenizer(line, " ");
/*     */ 
/* 357 */             this.sector1.triangle[i].vertex[vert].x = Float.valueOf(st.nextToken()).floatValue();
/* 358 */             this.sector1.triangle[i].vertex[vert].y = Float.valueOf(st.nextToken()).floatValue();
/* 359 */             this.sector1.triangle[i].vertex[vert].z = Float.valueOf(st.nextToken()).floatValue();
/* 360 */             this.sector1.triangle[i].vertex[vert].u = Float.valueOf(st.nextToken()).floatValue();
/* 361 */             this.sector1.triangle[i].vertex[vert].v = Float.valueOf(st.nextToken()).floatValue();
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/* 366 */       dis.close();
/*     */     }
/*     */     catch (IOException ioe)
/*     */     {
/* 370 */       ioe.printStackTrace();
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\DBDownload\lwjglDemo2\lwjglDemo2\Run.jar
 * Qualified Name:     com.nehe.learn.Lesson10
 * JD-Core Version:    0.6.2
 */